package cn.xi.controller;

import cn.xi.checkItem.MemberService;
import cn.xi.checkItem.OrderService;
import cn.xi.checkItem.ReportService;
import cn.xi.checkItem.SetmealService;
import cn.xi.constant.MessageConstant;
import cn.xi.entity.Result;
import cn.xi.pojo.Setmeal;
import com.alibaba.dubbo.config.annotation.Reference;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.*;

@RestController
@RequestMapping("/report")
public class ReportController {

    @Reference
    private MemberService memberService;
    @Reference
    private SetmealService setmealService;
    @Reference
    private OrderService orderService;

    @Reference
    private ReportService reportService;

//    @RequestMapping("/getMemberReport")
//    public Result getMemberReport() {
//        try {
//            Map map = new HashMap();
//            //创建出Calendar 的实列对象(因为是抽象类不能通过new出来)
//            //获得当前时间
//            Calendar calendar = Calendar.getInstance();
//            //根据日历的规则，为指定的日历添加或者减少指定的时间
//            //Calendar改变成当前时间的12个月前的时间
//            calendar.add(Calendar.MONTH, -12);
//            List<String> list = new ArrayList<>();
//            for (int i = 0; i < 12; i++) {
//                //Calendar的时间月数加1
//                calendar.add(Calendar.MONTH, 1);
//                //calendar.getTime();   将Calendar时间转换成Date格式
//                //将时间转换成字符串存到list集合中
//                list.add(new SimpleDateFormat("yyyy.MM").format(calendar.getTime()));
//            }
//            map.put("months", list);
//            List<Integer> list1 = memberService.selectTimeNumber(list);
//            map.put("memberCount", list1);
//            return new Result(true, MessageConstant.GET_MEMBER_NUMBER_REPORT_SUCCESS, map);
//        } catch (Exception e) {
//            e.printStackTrace();
//            return new Result(false, MessageConstant.GET_MEMBER_NUMBER_REPORT_FAIL);
//        }
//
//
//    }

    @RequestMapping("/getMemberReport")
    public Result getMemberReport() {
        try {
            Map map = memberService.selectTimeNumber2();
            return new Result(true, MessageConstant.GET_MEMBER_NUMBER_REPORT_SUCCESS, map);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.GET_MEMBER_NUMBER_REPORT_FAIL);
        }
    }


    @RequestMapping("/getSetmealReport")
    public Result getSetmealReport() {
        try {
            List<Map> list = setmealService.selectandorder();
            Map map = new HashMap();
            List<String> strings = new ArrayList<>();
            for (Map m : list) {
                strings.add((String) m.get("name"));
            }
            map.put("setmealNames", strings);
            map.put("setmealCount", list);
            return new Result(true, MessageConstant.GET_MEMBER_NUMBER_REPORT_SUCCESS, map);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.GET_MEMBER_NUMBER_REPORT_FAIL);
        }

    }


    @RequestMapping("/getBusinessReportData")
    public Result getBusinessReportData() {
        try {
            Map map = reportService.getBusinessReportData();
            return new Result(true, MessageConstant.GET_BUSINESS_REPORT_SUCCESS, map);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.GET_BUSINESS_REPORT_FAIL);
        }
    }

    @RequestMapping("/exportBusinessReport")
    public void exportBusinessReport(HttpServletRequest request, HttpServletResponse response) {
        Map map = reportService.getBusinessReportData();
        String reportDate = (String) map.get("reportDate");
        Integer todayNewMember = (Integer) map.get("todayNewMember");
        Integer totalMember = (Integer) map.get("totalMember");
        Integer thisWeekNewMember = (Integer) map.get("thisWeekNewMember");
        Integer thisMonthNewMember = (Integer) map.get("thisMonthNewMember");
        Integer todayOrderNumber = (Integer) map.get("todayOrderNumber");
        Integer todayVisitsNumber = (Integer) map.get("todayVisitsNumber");
        Integer thisWeekOrderNumber = (Integer) map.get("thisWeekOrderNumber");
        Integer thisWeekVisitsNumber = (Integer) map.get("thisWeekVisitsNumber");
        Integer thisMonthOrderNumber = (Integer) map.get("thisMonthOrderNumber");
        Integer thisMonthVisitsNumber = (Integer) map.get("thisMonthVisitsNumber");
        List<Map> hotSetmeal = (List<Map>) map.get("hotSetmeal");

        String path = request.getSession().getServletContext().getRealPath("template") + File.separator + "report_template.xlsx";
        try {
            XSSFWorkbook sheets = new XSSFWorkbook(new FileInputStream(new File(path)));
            XSSFSheet sheetAt = sheets.getSheetAt(0);

            XSSFRow row = sheetAt.getRow(2);
            row.getCell(5).setCellValue(reportDate);  //日期

            row = sheetAt.getRow(4);
            row.getCell(5).setCellValue(todayNewMember);//新增会员
            row.getCell(7).setCellValue(totalMember); //总会员

            row = sheetAt.getRow(5);
            row.getCell(5).setCellValue(thisWeekNewMember);//本周新增会员
            row.getCell(7).setCellValue(thisMonthNewMember);//本月新增会员

            row = sheetAt.getRow(7);
            row.getCell(5).setCellValue(todayOrderNumber); //今日预约数
            row.getCell(7).setCellValue(todayVisitsNumber);//今日到诊数

            row = sheetAt.getRow(8);
            row.getCell(5).setCellValue(thisWeekOrderNumber); //本周预约数
            row.getCell(7).setCellValue(thisWeekVisitsNumber);//本周到诊数

            row = sheetAt.getRow(9);
            row.getCell(5).setCellValue(thisMonthOrderNumber);//本月预约数
            row.getCell(7).setCellValue(thisMonthVisitsNumber);//本月到诊数

            int in = 12;
            for (int i = 0; i < hotSetmeal.size(); i++) {
                Map map1 = hotSetmeal.get(i);
                String name = (String) map1.get("name");
                Long setmeal_count = (Long) map1.get("setmeal_count");
                BigDecimal proportion = (BigDecimal) map1.get("proportion");
                row = sheetAt.getRow(in++);
                row.getCell(4).setCellValue(name); //套餐名字
                row.getCell(5).setCellValue(setmeal_count);//预约数量
                row.getCell(6).setCellValue(proportion.doubleValue());//占比
            }


//通过输出流进行文件下载
            ServletOutputStream out = response.getOutputStream();
            response.setContentType("application/vnd.ms-excel");
            response.setHeader("content-Disposition", "attachment;filename=report.xlsx");
            sheets.write(out);
            out.flush();
            out.close();
            sheets.close();


        } catch (Exception e) {
            e.printStackTrace();
        }

    }
}
